import { Employee, EmployeeParam } from '@/api/employee/model';
import request from '@/utils/request';
import { ApiResult, PageResult } from '@/api';
import md5 from 'js-md5';
/**
 * 添加管理员
 * @param data
 */
export async function addEmployee(data: Employee) {
  const res = await request.post<ApiResult<unknown>>('/employee', data);
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 更新管理员
 * @param data
 */
export async function updateEmployee(data: Employee) {
  const res = await request.put<ApiResult<unknown>>('/employee', {
    id: data.id,
    account: data.account,
    img: data.headImg,
    name: data.name,
    remark: data.remark
  });
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 * 重置密码
 * @param data
 */
export async function updateEmployeePassword(data: Employee) {
  const res = await request.put<ApiResult<unknown>>('/employee', {
    id: data.id,
    password: md5(data.password),
    name: data.name,
    status: data.status
  });
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 *
 * @param data 修改管理员状态
 * @returns
 */
export async function updateEmployeeStatus(id?: string, status?: boolean) {
  const res = await request.patch<ApiResult<unknown>>(
    '/employee/status',
    {},
    {
      params: {
        id: id,
        status: status
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 * 获取管理员列表
 * @param data
 */
export async function getEmployeeList(data: EmployeeParam) {
  const res = await request.get<ApiResult<PageResult<Employee>>>('/employee', {
    params: data
  });
  if (res.data.code === 0) {
    const pageData = {
      list: res.data.data?.records,
      count: res.data.data?.total
    };
    return pageData;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改管理员密码
 * @param id
 * @param status
 */
export async function updatePassword(
  oldPassword?: string,
  newPassword?: string
) {
  const res = await request.patch<ApiResult<unknown>>(
    '/employee/',
    {},
    {
      params: {
        oldPassword: oldPassword,
        newPassword: newPassword
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
